package com.laobiao.file_netty_lib.netty.client.handler;

import java.util.logging.Level;
import java.util.logging.Logger;

import com.laobiao.file_netty_lib.content.LoggerUtil;
import com.laobiao.file_netty_lib.netty.client.ClientBusiness;
import com.laobiao.file_netty_lib.netty.client.handler.fileList.ClientFileListBusiness;
import com.laobiao.file_netty_lib.packet.BasePacket;
import com.laobiao.file_netty_lib.packet.command.ResoultCommand;
import com.laobiao.file_netty_lib.packet.command.Mkdir.MkdirCommand;
import com.laobiao.file_netty_lib.packet.command.fileDelete.FileDeleteCommand;

import io.netty.channel.Channel;

public class ClientMkdirBusiness implements ClientBusiness{
	
private final Logger logger = LoggerUtil.getLogger(ClientFileListBusiness.class);
	
	private MkdirCommand command;
	
	
	private Channel channel;
	
	private volatile boolean stop = false;
	
	private volatile boolean download = false;
	
	private String msg = "";
	
	public ClientMkdirBusiness() {
	}

	public ClientMkdirBusiness( MkdirCommand command,
			Channel channel) {
		this.channel = channel;
		this.command = command;
		
	}

	@Override
	public void resoultPacket(BasePacket packet) {
		logger.log(Level.CONFIG,"收到服务端发来的反馈命令包！"+packet);
		/**
		 * 上传只会接收反馈命令包，其他包不管
		 */
		if (packet instanceof ResoultCommand) {
			ResoultCommand reCommand = (ResoultCommand) packet;
			if (reCommand.isStatus()) {//成功状态
				if (reCommand.getCode() == ResoultCommand.ResoultCode.Common.SERVER_RECEIVE) {// 接收过程中成功的状态信息
					
				} else if (reCommand.getCode() == ResoultCommand.ResoultCode.Common.SERVER_CHECK) {// 校验完成入库的成功信息
					//logger.info("文件"+localFile.getAbsolutePath()+"上传成功！");
					sucess(reCommand.getMessage());
					close();
				}
			} else {//失败状态
				//logger.warning("上传失败！"+reCommand.getMessage());
				err(reCommand.getMessage());
				close();
			}
		}
		
	}
	
	private void setStatus(boolean stop, boolean download, String msg) {
		this.stop = stop;
		this.download = download;
		this.msg = msg;
		if (stop) {// 停止
			close();
		}
	}
	
	private void err(String msg) {
		setStatus(true, false, msg);
	}
	
	private void sucess(String msg) {
		setStatus(true, true, msg);
	}

	@Override
	public Object executStatus(int executReturnTimeOutSeconds) {
		channel.writeAndFlush(command);
		return true;
	}

	@Override
	public void close() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Channel getChannel() {
		// TODO Auto-generated method stub
		return channel;
	}
	

}
